Docker Containers
A Docker container is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software: the code, runtime, libraries, environment variables, and configuration files. Containers are isolated from the host system and other containers, ensuring that they run consistently across various environments.
How Docker Containers Work
-
Docker Images: A container is created from a Docker image. An image is a read-only template with instructions for creating a container.
-
Containers: When you run a Docker image, it becomes a container. Containers can be started, stopped, moved, and deleted. Each container is isolated but can communicate with other containers or the host system.
-
Isolation: Containers use Linux kernel features like namespaces and cgroups to isolate processes, network interfaces, and file systems.
Step-by-Step Guide to Using Docker Containers
Step 1: Install Docker
Follow the installation guide I provided earlier to install Docker Desktop on your macOS or other operating systems.
Step 2: Pull a Docker Image
Before you can create a container, you need a Docker image. Images can be pulled from Docker Hub (a public repository of Docker images) or created locally.
Example to pull a MySQL image:
This command will pull the official MySQL image from Docker Hub.
Step 3: Run a Docker Container
Once the image is downloaded, you can run it as a container. Let’s run the MySQL image as a container.
Let’s break down the command:
-
--name mysql-container
: Assigns a name to the container. -
-e MYSQL_ROOT_PASSWORD=root
: Sets an environment variable for the MySQL root password. -
-d
: Runs the container in detached mode (in the background). -
mysql
: The image to run (the one you just pulled).
Step 4: List Running Containers
To check if your container is running, use:
This will show all running containers. You should see something like this:
Step 5: Access the Running Container
You can execute commands inside a running container using docker exec
.
For example, to access the MySQL shell inside the container, run:
-
exec -it
: Runs a command inside the container interactively. -
mysql-container
: The container name you created. -
mysql -u root -p
: Runs the MySQL command line with the root user, and-p
will prompt you for the password.
Step 6: Interact with the Container
Once inside the MySQL shell, you can run SQL commands like:
To exit the MySQL shell, type:
To exit the container’s interactive shell, type exit
in the terminal.
Step 7: Stop a Container
To stop a running container, use:
This will gracefully stop the MySQL container.
Step 8: Remove a Container
If you no longer need the container, you can remove it using:
This will delete the container. If the container is stopped, it will be removed.
To force remove a running container, you can use the -f
flag:
Step 9: Remove a Docker Image
To remove a Docker image from your local machine, first, stop and remove any containers based on that image, and then run:
This will remove the MySQL image from your local Docker environment.
Step 10: Docker Container Networking
Containers can communicate with each other and the outside world via networking.
Link Containers Together
For example, you can link a web app container to a MySQL container by connecting them to the same Docker network.
-
Create a custom network:
-
Run a MySQL container in the custom network:
-
Run a web application container (e.g., Nginx or a custom app) in the same network:
Now, both containers can communicate with each other using the container names (mysql-container
, web-container
).
Step 11: Docker Volumes (Persistent Storage)
By default, data stored inside a container is lost when the container is removed. To preserve data, you can use Docker volumes.
Example to create and mount a volume for MySQL data:
This creates a volume named mysql-data
and mounts it inside the MySQL container at /var/lib/mysql
to persist data across container restarts.
Managing Docker Containers with Docker Compose
Docker Compose is used to define and run multi-container Docker applications. Instead of running containers one by one, you define all services in a docker-compose.yml
file.
Here’s an example docker-compose.yml
file to run a MySQL and Nginx container together:
Run the containers with Docker Compose:
-
Create the
docker-compose.yml
file. -
Run the following command in the directory where the file is located:
This will start both the MySQL and Nginx containers.
Summary of Docker Container Commands
-
Pull an image:
docker pull <image-name>
-
Run a container:
docker run <options> <image-name>
-
List running containers:
docker ps
-
Execute command in container:
docker exec -it <container-name> <command>
-
Stop a container:
docker stop <container-name>
-
Remove a container:
docker rm <container-name>
-
Remove an image:
docker rmi <image-name>