0% found this document useful (0 votes)
5 views3 pages

TP Docker

This document provides a tutorial on creating and managing Docker containers and images, including commands for running, retrieving, and removing containers. It also covers setting up a static web service and creating a Docker image from a Dockerfile. Key commands such as 'docker run', 'docker pull', and 'docker build' are highlighted for practical use.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

TP Docker

This document provides a tutorial on creating and managing Docker containers and images, including commands for running, retrieving, and removing containers. It also covers setting up a static web service and creating a Docker image from a Dockerfile. Key commands such as 'docker run', 'docker pull', and 'docker build' are highlighted for practical use.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PRISTINI

Cloud Computing
A.U. : 2024/2025
Taecher : Rahma Haouas

Creating the Virtual Machine from a Box File


• Create a new directory tpdocker
• Create a new box boxdocker from the file docker_box.box
• Connect to this virtual machine

I) Basic Commands
Test Docker
docker run hello-world

Retrieve an Image
docker pull busybox

The pull command retrieves the busybox image from the Docker registry and saves it to our
system.

Display the List of Images

You can use the following command to display a list of all images available on your system:

docker images

Run a Docker Container

docker run busybox


When you run docker run, the Docker client locates the image (in this case, busybox), loads
the container, and executes a command inside it.
Since we didn’t provide any command when executing docker run busybox, the container
started, ran an empty command, and then stopped.
Try the following command:

docker run busybox echo "hello from busybox"


If you noticed, this happened quite quickly. Imagine starting a virtual machine, running a
command, and then shutting it down.

Display the List of Running Containers


docker ps
docker ps -a
Run a Container in Interactive Mode

Running the run command with the -it flags attaches an interactive TTY to the container. This
allows you to execute multiple commands inside the container.

docker run -it busybox sh


ls
uptime
Use docker run --help to see a list of all supported flags.

Remove a Container
We saw earlier that we can still see remnants of a container even after it exits by running
docker ps -a.
Throughout this tutorial, you’ll run multiple containers, and leaving unused containers will
consume disk space.
As a general rule, it’s good practice to clean up containers once you're done with them.

docker rm 305297d7a235 ff0a5c3750b9


docker rm $(docker ps -a -q -f status=exited)
The second command removes all containers with exited status.
In case you’re wondering:
• The -q flag returns only numeric container IDs.
• The -f flag filters the output based on the specified condition.
Note: The --rm option with run automatically removes the container once it stops.
In later Docker versions, you can use docker container prune to achieve the same effect.

docker container prune

II) Running a Static Web Service


Download and Run the Container
docker run --rm prakhar1989/static-site
docker pull prakhar1989/static-site:latest

How to Access the Site?

How can we access the container from the host machine?


Press Ctrl+C to stop the container.

docker run -d -P --name static-site prakhar1989/static-site


• -P : Maps all exposed ports to random ports.
• -d : Detaches the terminal, allowing you to close it while keeping the container
running.

Display Used Ports


docker port static-site
You can also specify a custom port for the host to forward connections to the container:

docker run -p 8888:80 prakhar1989/static-site

Stop the Container

docker stop static-site


I’m sure you’ll agree that this was super simple! To deploy this on a real server, you just need
to install Docker and run the above command.

III) Creating a Docker Image


1. Create a new directory dockerfile
2. Navigate to this directory
3. Create a Dockerfile with the following content:
dockerfile

FROM ubuntu

MAINTAINER Firstname Lastname <[email protected]>

RUN apt-get update

CMD ["echo", "Hello World from my first Docker image"]

Build the Image

docker build -t my_image:1.0 .

Verify the Image


docker images

Run the Image


docker run <IMAGE ID>

You might also like