I’m still a beginner in Docker and still don’t have a clue what most of the instructions in Dockerfile or docker-compose.yml do. I just understand the simple ones. So to consolidate my understandings and also shed light for others, I decided to just make a very simple Dockerfile for you, presumably also a beginner to get to play in a new docker container in a quick and easy way!
FROM ubuntu:latest RUN apt-get update RUN apt-get install -y sudo RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo USER docker
This is the Dockerfile. Simple and neat.
Just open a directory then create this file named “Dockerfile”. Okay, now the explanations:
Quoting from the Docker Official Documentation:
A Docker image consists of read-only layers each of which represents a Dockerfile instruction. The layers are stacked and each one is a delta of the changes from the previous layer…Each instruction creates one layer.
RUN instruction will execute any commands in a new layer on top of the current image and result in a committed image for the next instruction.
First we do an
apt-get update to know the latest versions of the packages from the repositories and then we
apt-get install -y sudo, as the default image does not have it.
The third run does three things. First,
useradd -m docker to create the user docker and its home directory. Second,
echo "docker:docker" | chpasswd to set the password for the user docker (check this link). Third,
adduser docker sudo to add docker into the sudo group so that docker becomes a sudo-enabled user.
USER instruction sets the user to use when running the image and for any
ENTRYPOINT instructions that follows in the Dockerfile.
That’s all for the configurations part.
Now, time to run the image.
When you run an image and generate a container, you add a new writable layer (the “container layer”) on top of the underlying layers. All changes made to the running container, such as writing new files, modifying existing files, and deleting files, are written to this thin writable container layer.
First, build the image, with a name:
# format: docker build -t a_name_you_like Path_of_Dockerfile # example: docker build -t new-ubuntu .
So assuming your console is already at the Dockerfile’s directory, you can use . for the path of Dockerfile.
Second, run the image and give a name to the container:
# format: docker run -dt --name container_name image_name # example: docker run -dt --name new-ubuntu-ctnr new-ubuntu
Here we need two flags,
-d for detached mode so that it does not consume the foreground of your console and
-t for allocating a pseudo TTY. Okay, I admit that I don’t know what does -t mean here, but this flag can keep the container to continue running instead of exiting right after you run this command. And that’s important.
Now as the container is running, (you can check that by
docker container ls), you can now ssh into it and play around like it’s a new ubuntu computer!
# format: docker exec -it container_name command # example: docker exec -it new-ubuntu-ctnr bash
From the documentation, using flags
-it and executing bash as command will create a new interactive Bash session on the container. Voila!