Tinkering with Docker
Ok we have now created our first image and run our first application on it. But what exactly is going on within that image. Can we peel back the onion?
Yes we can by running it in interactive mode. Basically, we will use some form of docker run -it tag_name
to do this.
Different images will behave differently with this command. Some will start running in bash, some in python, some in something else. It all depends on what is on the image.
For our nginx:alpine
image, we need to specify that we are using sh
to look around.
docker run -it html /bin/sh
If you run ls
, you will be able to see the contents of the image:
bin media srv
dev mnt sys
docker-entrypoint.d opt tmp
docker-entrypoint.sh proc usr
etc root var
home run
lib sbin
This can be really helpful to figuring out when something goes wrong.
To leave interactive mode:
exit
Containers vs Images
Here is a bit of terminology that shouldn't matter that much but might depending on who you are talking to— containers vs. images.
Think of docker images as templates. You don't run the image but you use that image as a set of instructions to build a container. A container therefore is just a running image. When you create a container, it creates a writable layer on top of the image, thus allowing modifications.
Managing Running Containers
Eventually, you will have a bunch of images and you may find it hard to keep track of them. Luckily there is a command to see what containers are running:
docker ps
There is also a way to all images regardless of whether or not they are running:
docker ps -a
You should get something like the following after running one of these commands:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
195951d16831 html "/docker-entrypoint.…" 39 minutes ago Up 39 minutes 0.0.0.0:8080->80/tcp trusting_engelbart
Notice how there is a container id
and an image name
? That's because a container is like a running instance of the image template. It's an image in action.
A container can be stopped with:
docker stop 195951d16831
And then restarted with:
docker start 195951d16831
But if you wanted to create a new container you would need to use:
docker run html
To be entirely clear, docker start
starts a container and docker run
creates an new container from the image.
Docker Run Options
I am not going to walk through every option that you can add to docker run but here is a table of a few key ones:
Option | Description |
---|---|
-d , --detach | Run the container in the background and print the container ID. |
--name | Assign a custom name to the container for easier identification. |
-p , --publish | Map a container's port(s) to the host (e.g., -p 8080:80 maps port 80 in the container to port 8080 on the host). |
-v , --volume | Mount a host directory or volume into the container (e.g., -v /host/path:/container/path ). |
-e , --env | Set environment variables inside the container (e.g., -e MY_VAR=value ). |
-it | Run in interactive mode with a pseudo-TTY allocated, allowing you to interact with the container's shell. |
--rm | Automatically remove the container when it exits. |
--restart | Set a restart policy for the container (e.g., always, unless-stopped). |
--read-only | Mount the container's root filesystem as read-only. |
--shm-size | Set the size of /dev/shm , which is useful for applications that require shared memory. |