What is a Docker Container - A Complete Guide
In today’s development landscape, the ability to deploy projects efficiently and make them accessible for collaboration significantly enhances their potential for growth. Docker revolutionizes project sharing and setup by allowing developers to create images, push them to repositories, and enable quick access for other developers. Setting up becomes a breeze—just run a few commands, and you’re good to go.
Before we dive into Docker image creation, let’s discuss some essential Docker commands.
Note: If you want to learn what Docker is and how to set up Docker locally, read these articles: What is Docker and How to Set Up Docker.
Essential Docker Commands
Docker has many commands, but as developers, we only need a few that we will use repeatedly. While Docker has a long list of commands, in this tutorial, we will focus on a few basic ones.
The basics of Docker are divided into the following three parts:
- Docker file
- Docker Image
- Containers
These are some common docker commands that you will be using while working on your projects. Let’s first look at some basic commands in Docker:
- Docker build command
This command creates a Docker image by using the docker file.
docker build dockerFile_path
- Docker pull command
This command is used to pull images from the docker hub.
docker pull image_name
- Docker run command
Helps us run the image file and create the containers. Later, we will discuss what -it
and -d
does.
docker run –it –d container_id/name
- Docker ps command
Lists all the running containers.
docker ps
- Docker image ls command
List all the images.
docker image ls
- Docker stop command
Stop the running container.
docker stop container_id/name
- Docker exec command
Executes a running container.
docker exec container_id/name
- Docker kill command
Kills a running container. We usually use docker kill
when a container is taking too much time to close.
docker kill container_id/name
Docker push command
This command will push the image to the docker hub.
docker push username/image_name
How to create a Docker file
A docker file is a simple text file that contains instructions for creating an image file. The following is how we can create a Docker file for a Python project:
Create a new folder wherever you want to learn Docker and there all the files related to Docker will be created.
In the project folder create a new file and name it “dockerfile”. For this tutorial, we’ll use the BMI calculator coded in Python to create the docker image.
We start a Dockerfile by specifying the base image. For a Python project, use:
FROM python
This line tells Docker to use the official Python image as the starting point for our container and this will provide a pre-configured environment with Python already installed.
If we need a specific Python version, we can specify it like this:
FROM python:3.9
This ensures our project runs on a particular Python version, which can be important for compatibility and reproducibility.
To see the supported versions, you can check out Docker Hub.
- Create a working directory for our image. This will make our
Dockerfile
more organized, readable, and maintainable.
FROM pythonWORKDIR /myapp
Note: If we don’t create a WORKDIR in your Dockerfile, all commands will execute in the container’s root directory (/) by default.
- We can copy local files into the container’s working directory using the
COPY command
:
FROM pythonWORKDIR /myappCOPY . /myapp
The use of the dot is to copy all files from our local directory into the “/myapp” directory in the container.
Alternatively, we can specify individual files or directories:
COPY requirements.txt /app/COPY src/ /app/src/
Using COPY ensures that our application files are available inside the container at the specified location.
- Add the
CMD
instruction to specify the command that runs when the container starts:
FROM pythonWORKDIR /myappCOPY . /myappCMD ["python3", "myapp.py"]
This tells Docker to execute your Python script when the container launches.
The difference between CMD and RUN:
CMD: Runs when you start the container. Use it for the main command that your application needs to run.
RUN: Executes during the image build process. Use it for setup tasks like installing dependencies:
RUN pip install -r requirements.txt
RUN commands modify the image, while CMD sets the default command for running the container.
How to create a Docker image
A Docker image is a file with all the dependencies and libraries required to run the program. Here are different ways to create an image from the Dockerfile:
- Create an image without giving it a name or tag:
docker build .
If the Dockerfile is in a different directory, specify the path:
docker build /path/to/dockerfile/directory
For example, if our Dockerfile is in a directory named “demo”:
docker build ./demo
- Verify the image creation:
docker image ls
This command lists all images present in our system. Our recently created image will be shown at the top.
- Create a named image with a specific tag:
You must have noticed the Repository
and Tag
columns in the docker image ls
output:
- The
Repository
column shows the image name - The
Tag
column indicates the image version
This is how you can add a name and a version to our Docker image:
docker build -t pythoncode:01 .
This format creates an image named “pythoncode” with the tag “01”.
Here’s an improved version of the note on Docker tagging and versioning:
Note: Docker tags and versioning enable running multiple versions of an application simultaneously in separate containers. To create and manage versions:
- Pull an existing image or create a new one.
- Make desired changes to the image or its underlying application.
- Build a new image with a unique tag (e.g.,
myapp:v1.2.3
). - Repeat steps 2-3 for each new version.
This approach allows developers to:
- Test multiple versions of a project concurrently
- Easily switch between versions
- Maintain a clear history of application changes
- Ensure reproducibility across different environments
Example workflow:
docker pull myapp:latest# Make changes to the applicationdocker build -t myapp:v1.0.0 .# Make more changesdocker build -t myapp:v1.1.0 .
You can then run different versions side by side:
docker run -d myapp:v1.0.0docker run -d myapp:v1.1.0
How to create Docker Containers
Containers are instances of Docker images. After creating an image, the next step is to run containers from it. Here’s how to do it effectively:
- List available images:
docker image ls
- Create an interactive container:
For projects requiring user interaction, use the -it
flags:
docker run -it image_name:tag
Our current requires user interaction to calculate the BMI, so we’ll use this command:
docker run -it pythonapp:01
The following are key options when running containers:
Detached mode (-d
): Run containers in the background and helps us run multiple containers at once:
docker run -d image_name:tag
Port mapping (-p
): Map host ports to container ports for network access, enabling us to access the containerized application as if it were running directly on our host machine:
docker run -d -p host_port:container_port image_name:tag
Example: docker run -d -p 3001:3000 myapp:latest
This maps:
Host machine (localhost:3001) <-----> Container (container_ip:3000)
Auto-remove (--rm
): Automatically remove the container when it exits:
docker run -d --rm -p 3000:3000 image_name:tag
Custom naming (--name
): Assign a custom name to the container:
docker run -d --rm --name "my_custom_name" -p 3000:3000 image_name:tag
Combining the above options for a full example:
docker run -d --rm --name "web_app" -p 3001:3000 myapp:v1.2.3
This command:
- Runs a detached container (
-d
) - Auto-removes it when stopped (
--rm
) - Names it “web_app”
- Maps port 3001 on the host to 3000 in the container
- Uses the image “myapp” with tag “v1.2.3”
Conclusion
Docker is an amazing tool for running applications seamlessly on your system using Docker containers, whether it’s your application or someone else’s. It helps you set up the dependencies, libraries, and files in your system and run as many versions of it as you want. In this tutorial, we covered:
- What are docker files and how to create them based on our project.
- What images are and how to create our own image file.
- What containers are and where to use which instructions.
For further reading, feel free to explore our Intro to Docker and Setting up Docker tutorials.
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
Learn more on Codecademy
- Skill path
Code Foundations
Start your programming journey with an introduction to the world of code and basic concepts.Includes 5 CoursesWith CertificateBeginner Friendly4 hours - Career path
Full-Stack Engineer
A full-stack engineer can get a project done from start to finish, back-end to front-end.Includes 51 CoursesWith Professional CertificationBeginner Friendly150 hours