Now that we know how to get up and running with Docker, lets do a little more playing around with some docker commands and discuss their functionalities. I will try to be as simple, clear and straight to the point.
I have intentionally omitted some commands because they exist in the first part of this tutorial
So, last time we used the
pull command to get our images from the Docker Hub before starting and attaching to it. This process can be reduced into one simple step. The command below will download the image, run it, attach to it and start the interpreter all at once.
docker run -ti alpine sh
As you can see above, the run command first of all checks if you have the Docker image available on your local machine, and if not, pulls it from the Hub. Furthermore it launches the interpreter when it's done.
You must have noticed the optional parameters. Well, let me explain them.
-ti can also be expressed as
-t -i. The
-t stands for tty (or terminal) while
-i is for interactive mode. sh though stands for shell.
With this knowledge, why don't we just launch bash in the terminal instead.
Straight away what comes to mind is to try this
docker run -ti alpine bash but two things happen here. First you end up creating a new container (which you should know from the first tutorial) and also you get an error that looks like the one below.
This is because bash is not installed in that container. So how do we fix this?
Well, run the
ps -a command to find a list of your containers. Now delete the last one with the error as it's of no use to us any more. The first instance however, you should start and then use the command below to launch the container in shell mode.
docker exec -ti 89fd sh
Now that we have the shell running, lets use the special package manager of the alpine container apk to install bash. First of all run
apk update then
apk add bash. This downloads and installs bash from the repo. You can now type in
bash to go into the bash mode to confirm you've done the right thing. Now type in
exit twice. First to exit bash and the other to exit sh. The Docker container should still be running this time but in the background. To go straight to bash mode, since we now have it installed in our container, enter;
docker exec -ti 89fd bash
I leave the murder of that container to you (exit and kill the container if you wish).
Up Next: Volumes and Networks