Open a shell (using an application like Terminal or iTerm), and run the following command:
docker run -it ubuntu:16.04 bash
Let's take a look at what this command does.
The docker run command lets you start a container from an image. In this case, you are creating a container from the ubuntu:16.04 image. This is the ubuntu image with the 16.04 tag.
Each Docker container should run a single process. So the docker run command lets you specify a single command to run inside of the container. In our case, we are going to run a bash shell. So we've specified the command to run as bash. Since we are running a bash shell we are going to want to allocate a pseudo-TTY and keep STDIN open. This is why we've added the options -it to the run command.
After running this command you should see something like this:
root@69129699ac86:/#
This is the bash shell running inside of the container you just created from the ubuntu image. You can run ps to see which processes are running in this container.
root@69129699ac86:/# ps
PID TTY TIME CMD
1 ? 00:00:00 bash
18 ? 00:00:00 ps
Type exit to close the shell. Now you can run docker ps to see which containers are currently running. You should find that there are no running containers. But if you run docker ps -a you'll see all containers including the one you just stopped. Since the containers are designed to run a single process, once that process is finished the container stops.
Take a look at the fields displayed by docker ps -a. Two important fields are CONTAINER ID and NAMES. If you want to interact with your containers you'll need to know the name or id.
If you want to start this container back up you can do so with docker start -ai <container id>
. Since the COMMAND associated with this container is bash we need to attach STDIN, STDOUT, and STDERR, hence the -ai options.
Theoretically we could run our container in the background. If our command was something else, say python manage.py runserver 0.0.0.0:8000, we would likely want to run our container in the background. In this case we would be able to see our container running when we type docker ps. If we want to run a command on an existing container we will use the docker exec command. The exec command offers familiar options, so if we wanted to open a shell in our django container we could type docker exec -it <container id> bash
.