howto:specific:learndocker
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
howto:specific:learndocker [2025/01/16 12:26] – Adding the first part of the workshop alexdavis | howto:specific:learndocker [2025/01/22 16:51] (current) – [Workshop tasks] alexdavis | ||
---|---|---|---|
Line 11: | Line 11: | ||
- Start with an application | - Start with an application | ||
- Write a **Dockerfile** that replicates the setup needed for the application | - Write a **Dockerfile** that replicates the setup needed for the application | ||
- | - Use the **Docker Client** to build your application into an **image** | + | - Use the **Docker Client** to build your application into an **image** using the **Dockerfile** |
- Then use the **Client** again to run the **image** as a **container** | - Then use the **Client** again to run the **image** as a **container** | ||
- Manage the running and configuration of multiple containers with **Docker Compose** | - Manage the running and configuration of multiple containers with **Docker Compose** | ||
+ | |||
+ | === Parts of the Docker architecture === | ||
+ | == Dockerfiles == | ||
+ | Dockerfiles are text files placed in the root of a project directory with instructions to build a **container**. They are comprised of **layers**, where each layer is modification to the image created by every layer " | ||
+ | < | ||
+ | FROM python: | ||
+ | |||
+ | # Install flask | ||
+ | WORKDIR /code | ||
+ | COPY requirements.txt /code | ||
+ | RUN pip3 install -r requirements.txt | ||
+ | |||
+ | # Copy the code to the image | ||
+ | COPY . . | ||
+ | |||
+ | # Open the flask port | ||
+ | EXPOSE 5000 | ||
+ | |||
+ | CMD [" | ||
+ | |||
+ | </ | ||
+ | Each line is a layer, and does the following: | ||
+ | - Loads the python 3.10 base image | ||
+ | - Moves into the /code directory | ||
+ | - Copies in the requirements file | ||
+ | - Installs the requirements | ||
+ | - Copies in the rest of the code | ||
+ | - Exposes the port the application uses | ||
+ | - Defines the command that runs when the container starts. | ||
+ | == Images == | ||
+ | Images are built from Dockerfiles. They are immutable collections of binaries, config files, libraries and other files that are needed to run a particular application. Base images are uploaded to registries and can save a lot of work in creating your own images. This ranges from particular operating system environments to entire ready-to-configure applications. | ||
+ | == Containers == | ||
+ | Containers are created from images. Containers are similar to virtual machines, but are not the same thing. They run applications in isolated, self-reliant environments. One of the advantages of Docker is that pretty much anywhere it runs, containers will run. | ||
+ | == Compose == | ||
+ | Docker Compose is an extra tool for managing the running of multiple-container applications. It makes things like networking easier, as well as configuration by storing parameters manually passed to //docker run// in a configuration file (normally called // | ||
+ | |||
+ | The // | ||
+ | < | ||
+ | version: " | ||
+ | services: | ||
+ | flask: | ||
+ | image: localhost/ | ||
+ | container_name: | ||
+ | ports: | ||
+ | - YourPort: | ||
+ | environment: | ||
+ | - DISPLAY_NAME=YourName | ||
+ | networks: | ||
+ | - external | ||
+ | |||
+ | # Below fixes a problem with Tardis container networking | ||
+ | networks: | ||
+ | external: | ||
+ | name: podman | ||
+ | external: true | ||
+ | </ | ||
+ | |||
+ | ==== Workshop tasks ==== | ||
+ | Below is the interactive tasks from the workshop for people to work through at their own pace. Familiarity with Linux and Git is expected. [[https:// | ||
+ | === Setup === | ||
+ | Sign up to Tardis if you haven' | ||
+ | Clone the workshop repo: | ||
+ | git clone https:// | ||
+ | Look inside the Dockerfile, what does each layer do? | ||
+ | === Build an image === | ||
+ | Run the below in the same directory as the Dockerfile to build the image. Change " | ||
+ | < | ||
+ | See the list of saved images with: | ||
+ | < | ||
+ | === Run a container === | ||
+ | Start your container with the following command, changing the display name argument and the port number. If the port number is in use run //docker rm flask// then try a different port. | ||
+ | < | ||
+ | docker run -e DISPLAY_NAME=YourName --name flask -p YourPort: | ||
+ | </ | ||
+ | You can check if the container is working properly by making a request to the port it uses, by listing your running containers and by checking the logs of the container. | ||
+ | < | ||
+ | curl localhost: | ||
+ | docker ps | ||
+ | docker logs flask | ||
+ | </ | ||
+ | Then stop and remove the container. Note that docker does not automatically remove stopped containers, you can see stopped containers with //docker ps -a//. | ||
+ | < | ||
+ | docker stop flask && docker rm flask | ||
+ | </ | ||
+ | === Configure your container with Compose === | ||
+ | Make sure your current container is stopped, then edit the // | ||
+ | docker-compose up -d | ||
+ | The //-d// flag detaches the container output from your terminal window. Check the output from the container with //curl localhost: | ||
+ | < | ||
+ | Curl the port again - it should have changed. Stop your container again with // | ||
+ | === Extra tasks to look into === | ||
+ | * Look into mounting and accessing a volume with Compose | ||
+ | * Open a shell in the container with //docker exec -it flask(-compose) sh// | ||
+ | * Try finding a docker image online and adding it as a service | ||
+ | * Find a piece of self-hosted software you can run as a Docker container |
howto/specific/learndocker.1737030408.txt.gz · Last modified: 2025/01/16 12:26 by alexdavis