Understanding Docker Commands: A Quick Guide

Understanding Docker Commands: A Quick Guide

Docker, the groundbreaking containerization platform, has revolutionized the way applications are developed, shipped, and run. If you’re new to Docker or just need a refresher, you’re in the right place. This blog will delve into some essential Docker commands you need to know.

1. Getting Started: Create MySQL Database Container

Before we begin, ensure Docker is installed on your machine. If not, follow https://kshitijaa.hashnode.dev/docker-for-devops-engineers-a-revolutionary-tool-in-software-delivery

This is one of the good scenario to deep into docker practically!

Here, We want to run a container that has mysql in it. Basically, we pull its image from docker hub

docker pull mysql:latest

When we pull any image, see its existence using following command:

docker images

Run(create container) using run command

docker run mysql

When run it is throwing an error that database is uninitialized and password option is not specified.

Let's run docker with specifying environment MYSQL_ROOT_PASSWORD

docker run -d -e MYSQL_ROOT_PASSWORD=test@123 mysql:latest

BOOM!!! Your MYSQL Container is Ready.

Check Container using docker ps, you see the status in the below image shows it is up!

docker ps

Now container is ready. How to access it?! Access this by going into this using following command

docker exec -it 99264c8781a0 sh #  it = interactive terminal 99264c8781a0 - container id

Do ls to see containers content

ls

Access database

mysql -u root -p
show databases;

Your MYSQL is READY!

2. Let's go through some Commands of Docker to use containers and images:

Use the docker run command to start a new container and interact with it through the command line. [Hint: docker run hello-world]

  1. This is a command to run a container and the image name is ubuntu, A container is created. To check a running container use the command docker ps and the container is up &running.

     docker run -itd --name kshitija ubuntu:latest /bin/bash
    

  2. Use the docker inspect command to view detailed information about a container or image.

  3. Use the docker port command to list the port mappings for a container.

    docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk11

      docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk11
    

    The -p arguments map a local port to a port exposed by the Docker container. The first argument is the local port, followed by a colon, and then the container port.

    So the argument -p 8080:8080 maps local port 8080 to the container port 8080 (which is the web port), and -p 50000:50000 maps local port 50000 to the container port 50000 (which is the agent port). This means you can open http://localhost:8080 on your local machine, and Docker directs the traffic into the webserver hosted by the container.

    The argument -v jenkins_home:/var/jenkins_home creates a volume called jenkins_home if it does not already exist and mounts it under the path /var/jenkins_home inside the container.

    Copy the public IP address of your instance and browse it with post 8080

  4. Use the docker stats command to view resource usage statistics for one or more containers.

docker stats

Type ctrl+c to come out of it. No one goona tell you this ;)

  1. Use the docker top command to view the processes running inside a container.

  2. Use the docker save command to save an image to a tar archive.

     docker save ubuntu:latest > /home/ubuntu/ubuntu.tar
    

  3. Use the docker load command to load an image from a tar archive.

     docker load < ubuntu.tar
    

Conclusion

The world of Docker offers much more than can be covered in one blog post. These commands, however, will give you a solid foundation to start with. As you become more comfortable with Docker, you’ll find that it simplifies many aspects of software development and deployment. The best way to learn is by doing, so fire up your terminal and start playing around with Docker!