2. Creating Docker Image and Running Container
A step-by-step guide to build and run you first docker image. This tutorial lets you learn different dockerfile commands to modify your image. This tutorial is designed for beginners and covers the essentials of Docker.
Introduction
In the previous blog post, we covered the installation of Docker on your system. Now that you have Docker up and running, it’s time to dive into the exciting world of containers. In this blog post, I’ll guide you through building your first Docker image and running your first container.
Before we begin, make sure you have Docker installed on your system. If you haven’t installed Docker yet, please refer to the previous blog post for installation instructions.
Creating Docker image and running container
Let’s start by creating a simple Docker image. We’ll use a basic Debian image as our base and install some essential packages.
- Create a Project Directory: Create a new directory for your Docker project. You can name it whatever you like. For this example, I’ll use
my-docker-app
.1 2
mkdir my-docker-app cd my-docker-app
- Create a Dockerfile: Inside your project directory, create a file named
Dockerfile
(no file extension). This file will contain instructions for building your Docker image.1
touch Dockerfile
- Write the Dockerfile: Open the
Dockerfile
in a text editor and add the following content: You can refer to this section for more information about the Dockerfile commands.1 2 3 4 5 6 7
# Use the official Debian image FROM debian:latest # Update the package list and install necessary packages RUN apt-get update && apt-get upgrade -y && \ apt-get install -y --no-install-recommends bash curl wget vim \ build-essential autoconf git wget python3 python3-pip && \ apt-get clean && rm -rf /var/lib/apt/lists/*
- Build the Docker image: Now, let’s build our Docker image using the
docker build
command. Make sure you are in the project directory where theDockerfile
is located.1
docker build -t my-docker-image .
This command will build the Docker image and tag it as
my-docker-image
. The.
at the end specifies the build context, which is the current directory. - Verify the image: After the build process completes, you can verify that your image was created successfully by running:
1
docker images
You should see your
my-docker-image
listed in the output. - Running your first Docker container: Now that we have our Docker image, let’s run a container based on it. To run a container from your image, use the
docker run
command:1
docker run -it --name my-container my-docker-image
This command will start a new container named
my-container
based on themy-docker-image
image. The-it
flag allows you to interact with the container’s shell. Once the container is running, you will be inside the container’s shell. You can now execute commands as if you were on a regular Debian system.
How to view running containers
To view the list of running containers, you can use the following command:
1
docker ps
This command will list all the running containers along with their details.
How to stop and remove the container
To stop the container, you can use the exit
command inside the container’s shell. If you want to remove the container, you can use the following command:
1
docker remove my-container
If you want to remove the image, you can use the following command:
1
docker rmi my-docker-image
Please make sure to remove old containers and images to free up space on your system. Docker usually takes significant space on your system.
How to open another shell in a running container
Sometimes you may want to open another shell inside a running container. You can do this using the docker exec
command. For example:
1
docker exec -it my-container /bin/bash
This command will open a new shell inside the running container.
How to attach a local directory to a container
If you want to attach a local directory to a container, you can use the -v
flag with the docker run
command. For example:
1
docker run -it -v /path/to/local/directory:/path/in/container my-docker-image
This will attach the local directory to the container at the specified path. You can modify the contents from both the host and the container and they will be in sync.
Understanding the Dockerfile Commands
In the Dockerfile we created, we used only two commands. However, there are many more commands available to customize your Docker image. Here are some of the most commonly used Dockerfile commands:
FROM
: Specifies the base image to use for your Docker image.RUN
: Executes a command inside the container during the build process. This is where you can install packages, run scripts, and perform other setup tasks.CMD
: Specifies the default command to run when the container starts.ENTRYPOINT
: Sets the default executable for the container.COPY
: Copies files from the host machine to the container.ADD
: Similar toCOPY
, but also supports remote URLs and tar file extraction.ENV
: Sets environment variables inside the container.
Here I have listed only few commands. You can find the complete list of Dockerfile commands in the official Docker documentation.
Conclusion
Now you have successfully built your first Docker image and run your first container. In the next blog post, we will explore Docker Hub and learn how to push and pull images from the Docker registry. Stay tuned!