Lecture 04 – BASIC DOCKER OPERATIONS

hands-on_image

BASIC DOCKER OPERATIONS

Basic Docker Operations involve managing and working with Docker containers. Docker is a platform that allows you to develop, deploy, and run applications using containers.

Here are some fundamental Docker operations:

1. – Pulling an Image: Docker containers are created from Docker images. To start, you need to pull the required Docker image from a registry (e.g., Docker Hub).

For example: docker pull image_name:tag

				
					docker pull sonarqube:latest
				
			

2. – Running and Port Mapping a Container: Once you have the image, you can run a container based on it and map/expose container ports to the host system.

For example: docker run -p 8080:80 image_name:tag

				
					docker run -p 9000:9000 sonarqube:latest
				
			
Docker basic ops (1)

Copy your Docker server IP:9000 to the browser and you should see below SonarQube UI.

Press Cntrl C to quit the running container.

3. – Listing Containers: To see a list of running containers, you can use the docker ps command and to see all containers ID, including stopped ones, add the -a flag:

For example: docker ps -a

				
					docker ps
				
			
				
					docker ps -a
				
			

4. – Starting Or Stop a Container: To start or stop a stopped container, use the docker start or stop command, followed by the container ID or name:

For example: docker start <containerID>

				
					docker start 816b6ca3c2a9
				
			
Docker basic ops (4)

5.- Executing Commands Inside a Container: You can run specific commands inside a running container using the docker exec command:

For example: docker exec -it container_id_or_name command_to_run

				
					docker exec -it 816b6ca3c2a9 bash
				
			

6. – Viewing Container Logs: To view the logs generated by a container, use the docker logs command, followed by the container ID or name:

For example: docker logs container_id_or_name

				
					docker logs 816b6ca3c2a9
				
			

7. – Removing a Container: To remove a stopped container, use the docker rm command, followed by the container ID or name:

For example: docker rm container_id_or_name

				
					docker rm aaf1d16b795a
				
			

8.Creates a new Docker image from a running container with the specified container ID: The “docker commit” command is used to save the current state of a container as an image, with the desired name for the new image.

For example: docker commit (containerid) (image-name)

				
					docker commit 816b6ca3c2a9 sonar
				
			

9. -Listing Docker Images: lists all the Docker images available on the local system. It displays a table that includes information such as the image ID, repository, tag, creation date, and size for each Docker image.

For example: docker images

				
					docker images
				
			

10. – Kill a Container: This command kills the container by stopping its execution immediately. The difference between ‘docker kill’ and ‘docker stop’. ‘docker stop’ gives the container time to shutdown gracefully, in situations when it is taking too much time for getting the container to stop, one can opt to kill it:

For example: docker kill <containerid>

				
					docker kill (containerid)
				
			

11. – Removing All Stopped Containers: This command remove all stop containers on your system.

For example: docker rm $(docker ps -aq)

				
					docker rm $(docker ps -aq)
				
			

-Removing All Images: This command will permanently delete all Docker images from your local system.

For example: docker rmi -f $(docker images -q)

				
					docker rmi -f $(docker images -q)