- Published on
Getting Started with Docker: Your First Container
- Authors
- Name
- Hieu Cao
Introduction
Docker provides a straightforward way to build, run, and manage containerized applications. In this guide, we'll walk through the steps to create and run your first Docker container while covering some essential Docker commands.
Prerequisites
Before getting started, ensure the following:
- Docker is installed on your system. Install Docker.
- Basic familiarity with the command line.
Step 1: Pulling a Docker Image
Docker images are the blueprint for containers. You can pull official images from Docker Hub.
Example: Pulling the Nginx Image
docker pull nginx
This command downloads the latest nginx
image from Docker Hub.
Step 2: Running a Docker Container
Once the image is downloaded, you can create and run a container from it.
Run the Nginx Container
docker run -d -p 8080:80 --name my-nginx nginx
Explanation:
-d
: Runs the container in detached mode (background).-p 8080:80
: Maps port 8080 on the host to port 80 in the container.--name my-nginx
: Names the containermy-nginx
.nginx
: Specifies the image to use.
Now, open your browser and visit http://localhost:8080
to see the Nginx welcome page.
Step 3: Basic Docker Commands
Viewing Running Containers
docker ps
This command lists all currently running containers.
Viewing All Containers
docker ps -a
Lists all containers, including stopped ones.
Stopping a Container
docker stop my-nginx
Run the Nginx Container Existing
docker start my-nginx
Stops the my-nginx
container.
Removing a Container
docker rm my-nginx
Deletes the my-nginx
container. Ensure the container is stopped before removing it.
Viewing Downloaded Images
docker images
Lists all images available on your system.
Removing an Image
docker rmi nginx
Removes the nginx
image.
Step 4: Running an Interactive Container
You can run a container interactively to execute commands inside it.
Example: Running an Ubuntu Container
docker run -it ubuntu
This command pulls the Ubuntu image (if not already downloaded) and starts a container with an interactive shell. You can now run commands inside the container.
Exit the container by typing:
exit
Step 5: Clean Up
To free up disk space, remove unused containers and images.
Remove Stopped Containers
docker container prune
Remove Unused Images
docker image prune
Conclusion
Creating your first Docker container is the initial step in mastering Docker's capabilities. By pulling images, running containers, and exploring basic commands, you're now equipped to experiment with Docker and integrate it into your development workflow.