Docker Container

Docker Container

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:

docker pull mysql

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.

docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=root -d mysql

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:

docker ps

This will show all running containers. You should see something like this:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1234abcd5678 mysql "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 3306/tcp, 33060/tcp mysql-container

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:

docker exec -it mysql-container mysql -u root -p
  • 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:

CREATE DATABASE my_database; SHOW DATABASES;

To exit the MySQL shell, type:

exit;

To exit the container’s interactive shell, type exit in the terminal.

Step 7: Stop a Container

To stop a running container, use:

docker stop mysql-container

This will gracefully stop the MySQL container.

Step 8: Remove a Container

If you no longer need the container, you can remove it using:

docker rm mysql-container

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:

docker rm -f mysql-container

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:

docker rmi mysql

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.

  1. Create a custom network:

    docker network create my-network
  2. Run a MySQL container in the custom network:

    docker run --name mysql-container --network my-network -e MYSQL_ROOT_PASSWORD=root -d mysql
  3. Run a web application container (e.g., Nginx or a custom app) in the same network:

    docker run --name web-container --network my-network -d nginx

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:

docker volume create mysql-data docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=root -v mysql-data:/var/lib/mysql -d mysql

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:

version: '3' services: mysql: image: mysql environment: MYSQL_ROOT_PASSWORD: root ports: - "3306:3306" nginx: image: nginx ports: - "8080:80" depends_on: - mysql

Run the containers with Docker Compose:

  1. Create the docker-compose.yml file.

  2. Run the following command in the directory where the file is located:

    docker-compose up

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>

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close