Managing Containers with Docker – OpenShift Tutorial Series

Managing Containers with Docker

Background

Docker Hub is a service provided by Docker for finding and sharing container images with your team. Features:

  • Repositories: Push and pull container images.
  • Teams & Organizations: Manage access to private repositories of container images.
  • Official Images: Pull and use high-quality container images provided by Docker.
  • Publisher Images: Pull and use high- quality container images provided by external vendors. Certified images also include support and guarantee compatibility with Docker Enterprise.
  • Builds: Automatically build container images from GitHub and Bitbucket and push them to Docker Hub.
  • Webhooks: Trigger actions after a successful push to a repository to integrate Docker Hub with other services.

Objectives:

  • Sign up at Docker Hub
  • Download Hello-world Image from Registry
  • Search for an Image in Registry
  • Pull MySQL image and run mysql docker container
  • Gain access to MySQL Server and container shell.
  • Listing Containers and Removing Containers

Pre-requisite

  • Local CentOS 7 VM with root access.
  • Docker-Engine pre-installed.

Sequence 1. Sign up at Docker Hub.

  1. Visit https://hub.docker.com and create an account.
  2. Create your first repository by clicking on Create a Repository on the Docker Hub welcome page:

  1. Name it <your-username>/openshift as shown below. Select Private:

  1. You’ve created your first repo. You should see:

Sequence 2. Working with Docker Containers

Docker containers are run from Docker images. By default, it pulls these images from Docker Hub, a Docker registry managed by Docker, the company behind the Docker project. Anybody can build and host their Docker images on Docker Hub, so most applications and Linux distributions you’ll need to run Docker containers have images that are hosted on Docker Hub.

  1. To check whether you can access and download images from Docker Hub, type:

# docker run hello-world

  1. The output, which should include the following:
  2. You can search for images available on Docker Hub by using the docker command with the search subcommand. For example, to search for the MySQL image, type:

 

# docker search mysql

  1. The script will crawl Docker Hub and return a listing of all images whose name match the search string. In this case, the output will be similar to this:

In the OFFICIAL column, OK indicates an image built and supported by the company behind the project.

  1. Once you’ve identifed the image that you would like to use, you can download it to your computer using the pull subcommand:

# docker pull mysql

  1. To see the images that have been downloaded to your computer, type:

# docker images

  1. The output should look similar to the following:

 Sequence 3. Running a Docker Container

Containers can be interactive. After all, they are similar to virtual machines, only more resource-friendly. Let’s run a container using MySQL image. The combination of the -i and -t switches gives you interactive shell access into the container:

Starting a MySQL Server Instance

  1. To start a new Docker container for a MySQL Server, use:

# docker run –name=mysql1 -d mysql/mysql-server:8.0

  1. Initialization for the container begins, and the container appears in the list of running containers when you run the docker ps For example:

# docker ps

  1. The -d option used in the docker run command above makes the container run in the background. Use this command to monitor the output from the container:

# docker logs mysql1

 

  1. Check the password with:

# docker logs mysql1 2>&1 | grep ‘GENERATED ROOT PASSWORD’

  1. Let us connect to MySQL Server from within the Container. Use the docker exec -it command to start a mysql client inside the Docker container you have started:

# docker exec -it mysql1 mysql -uroot -p

  1. To have shell access to your MySQL Server container, use docker exec -it command to start a bash shell inside the container:

# docker exec -it mysql1 bash

bash-4.2#

Sequence 4. Stopping and Deleting a Container

  1. To stop the MySQL Server container, use:

# docker stop mysql1

docker stop sends a SIGTERM signal to the mysqld process, so that the server is shut down gracefully.

Also notice that when the main process of a container (mysqld in the case of a MySQL Server container) is stopped, the Docker container stops automatically.

  1. To start the MySQL Server container again:

# docker start mysql1

  1. To stop and start again the MySQL Server container with a single command:

# docker restart mysql1

  1. To delete the MySQL container, stop it first, and then use the docker rm command:

# docker stop mysql1

# docker rm mysql1

Sequence 5. Listing Docker Containers

  1. After using Docker for a while, you’ll have many active (running) and inactive containers on your computer. To view the active ones, use:

# docker ps

  1. To view all containers — active and inactive, pass it the -a switch:

# docker ps -a

  1. To view the latest container you created, pass it the -l switch:

# docker ps -l

  1. Stopping a running or active container is as simple as typing:

# docker stop <container-id>

# docker rm <container id>

  1. Remove one Container

# docker rm <container id>

  1. Complete Clean up. Remove all images and cache.

# docker rmi -f $(docker images -a -q)

# docker system prune

February 5, 2021
Designed by  © Alliance Softech Pvt Ltd. All rights reserved.
WhatsApp chat
X