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.
- Get hands-on with Docker! Explore containers, Dockerfiles, workflows, and use cases while learning resource management for consistent, scalable deployment.
- Beginner Friendly.1 hour1 hour
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_pathCopy to clipboardCopy to clipboard
- Docker pull command
This command is used to pull images from the Docker Hub.
docker pull image_nameCopy to clipboardCopy to clipboard
- 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/nameCopy to clipboardCopy to clipboard
- Docker ps command
Lists all the running containers.
docker psCopy to clipboardCopy to clipboard
- Docker image ls command
List all the images.
docker image lsCopy to clipboardCopy to clipboard
- Docker stop command
Stop the running container.
docker stop container_id/nameCopy to clipboardCopy to clipboard
- Docker exec command
Executes a running container.
docker exec container_id/nameCopy to clipboardCopy to clipboard
- 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/nameCopy to clipboardCopy to clipboard
Docker push command
This command will push the image to the Docker Hub.
docker push username/image_nameCopy to clipboardCopy to clipboard
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 pythonCopy to clipboardCopy to clipboard
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.9Copy to clipboardCopy to clipboard
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
Dockerfilemore organized, readable, and maintainable.
FROM pythonWORKDIR /myappCopy to clipboardCopy to clipboard
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 . /myappCopy to clipboardCopy to clipboard
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/Copy to clipboardCopy to clipboard
Using COPY ensures that our application files are available inside the container at the specified location.
- Add the
CMDinstruction to specify the command that runs when the container starts:
FROM pythonWORKDIR /myappCOPY . /myappCMD ["python3", "myapp.py"]Copy to clipboardCopy to clipboard
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.txtCopy to clipboardCopy to clipboard
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 .Copy to clipboardCopy to clipboard
If the Dockerfile is in a different directory, specify the path:
docker build /path/to/dockerfile/directoryCopy to clipboardCopy to clipboard
For example, if our Dockerfile is in a directory named “demo”:
docker build ./demoCopy to clipboardCopy to clipboard
- Verify the image creation:
docker image lsCopy to clipboardCopy to clipboard
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
Repositorycolumn shows the image name - The
Tagcolumn indicates the image version
This is how you can add a name and a version to our Docker image:
docker build -t pythoncode:01 .Copy to clipboardCopy to clipboard
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 .Copy to clipboardCopy to clipboard
You can then run different versions side by side:
docker run -d myapp:v1.0.0docker run -d myapp:v1.1.0Copy to clipboardCopy to clipboard
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 lsCopy to clipboardCopy to clipboard
- Create an interactive container:
For projects requiring user interaction, use the -it flags:
docker run -it image_name:tagCopy to clipboardCopy to clipboard
Our current requires user interaction to calculate the BMI, so we’ll use this command:
docker run -it pythonapp:01Copy to clipboardCopy to clipboard
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:tagCopy to clipboardCopy to clipboard
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:tagCopy to clipboardCopy to clipboard
Example: docker run -d -p 3001:3000 myapp:latest
This maps:
Host machine (localhost:3001) <-----> Container (container_ip:3000)Copy to clipboardCopy to clipboard
Auto-remove (--rm): Automatically remove the container when it exits:
docker run -d --rm -p 3000:3000 image_name:tagCopy to clipboardCopy to clipboard
Custom naming (--name): Assign a custom name to the container:
docker run -d --rm --name "my_custom_name" -p 3000:3000 image_name:tagCopy to clipboardCopy to clipboard
Combining the above options for a full example:
docker run -d --rm --name "web_app" -p 3001:3000 myapp:v1.2.3Copy to clipboardCopy to clipboard
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.
'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
- Get hands-on with Docker! Explore containers, Dockerfiles, workflows, and use cases while learning resource management for consistent, scalable deployment.
- Beginner Friendly.1 hour1 hour
