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.
- Visit https://hub.docker.com and create an account.
- Create your first repository by clicking on Create a Repository on the Docker Hub welcome page:

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

- 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.- To check whether you can access and download images from Docker Hub, type:
- The output, which should include the following:
- 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:
- 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:

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

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

- 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
- To start a new Docker container for a MySQL Server, use:

- 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

- 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:
- Check the password with:

- 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:

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

Sequence 4. Stopping and Deleting a Container
- To stop the MySQL Server container, use:
- To start the MySQL Server container again:
- To stop and start again the MySQL Server container with a single command:
- To delete the MySQL container, stop it first, and then use the docker rm command:
Sequence 5. Listing Docker Containers
- After using Docker for a while, you’ll have many active (running) and inactive containers on your computer. To view the active ones, use:
- To view all containers — active and inactive, pass it the -a switch:
- To view the latest container you created, pass it the -l switch:
- Stopping a running or active container is as simple as typing:
- Remove one Container
- Complete Clean up. Remove all images and cache.