Containers Docker - Genocs/documentations GitHub Wiki

This page provide some useful info about to start using Docker

Install Docker based on the Host OS. Try this link in order to install Docker on Windows 10 Docker

How to set proxy

  1. Set environmental variable HTTP_PROXY
  2. Restart the server
Restart-Service docker

How run login

docker login

How to switch container type

Following command allows toggling between Windows and Linux

& $Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchDaemon

How check images list available locally

docker images

How show runninng container

Flag --all show even not running

docker container ls --all

List of useful commands

Following commands do:

  1. Stop running containers
  2. Delete all the containers
  3. Delete all the images
  4. Delete all the images (force delete) very usefull when an image has multiple references
  5. Restart container
  6. Remove image
  7. Stop container
  8. Remove every not running container
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
docker container prune
docker rmi $(docker images -q) -f
docker container restart 42dd3725810b
docker rmi 6c367cf4cb98
docker stop f93f9e2d7ba8

Docker compose

docker-compose --help
docker-compose version
docker-compose up

Docker network

Official documentation at Docker network

If you are running your containers in a user-defined network, then Docker maintains a DNS server that will map container names to addresses. That is, if I first create a network: [user-defined network]((https://docs.docker.com/engine/userguide/networking/)

Following command do:

  1. List the available Docker network
  2. Crete a network
  3. Remove unused networks
docker network ls
docker network create myapp_net
docker network prune

Docker Volumes

Docker allows to manage volumes as external data folder. These volumes can be either folder managed by host or logical volumes managed by Virtual Machine. Use following link to a complete list of commands Docker Volumes

docker volume ls
docker volume create my-vol
docker volume prune
docker volume rm

-v "$PWD/neo4j":/usr/share/neo4j/data --volume=$HOME/neo4j:/usr/share/neo4j/data -v my-vol:/usr/share/neo4j/data

How run shell inside container

docker exec -it compose_rabbitmq_1 bash

How get an image from repository (default: leatest version)

docker pull microsoft/aspnet
docker pull microsoft/nanoserver

Procedura per installare l'immagine di ASP.NET (Leatest Version)

docker inspect 1b4e1cd7ddb6
docker run debian echo "Welcome to Docker"
docker run -it debian /bin/bash

Esempio completo

Following command coming from stormpath.com

Setting up Docker It’s easy to get started with Docker. First, you have to install the Docker Engine on your machine (or your server). Follow the official instructions for Windows 10, Mac, or Linux.

For my own testing, I installed Docker for Windows on my Windows 10 development environment, and also on my Mac. To make sure the service is installed correctly, run this from the terminal:

docker --version

Docker version 1.12.0-rc4, build e4a0dbc, experimental If you see a version number, everything is working!

Creating an ASP.NET Core project

If you don’t already have the latest .NET Core tooling installed, grab that at the official .NET Core site. Once it’s installed on your machine, you can create a new directory and scaffold a new ASP.NET Core project easily:

mkdir AspNetCoreHelloWorld
cd AspNetCoreHelloWorld
dotnet new web --framework netcoreapp1.1

To make sure everything is working, try running the project locally first:

dotnet restore
dotnet run

The project should start listening on http://localhost:5000. Try it out in your browser!

Building a Dockerfile for ASP.NET Core Docker needs a Dockerfile that contains the “recipe” for creating an image based on the project. Create a new file called Dockerfile in the project directory:

touch Dockerfile

On Windows, you can run notepad Dockerfile instead, or use your favorite text editor. Make sure that this file isn’t saved with an extension like .txt – it’s supposed to have no extension.

The Dockerfile contents are straightforward:

FROM microsoft/dotnet:2.0.0-sdk

ENV http_proxy 192.168.2.7:3128
ENV https_proxy 192.168.2.7:3128

COPY . /app
WORKDIR /app

RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]

EXPOSE 5000/tcp
ENV ASPNETCORE_URLS http://*:5000

ENTRYPOINT ["dotnet", "run"]

Here’s what each of these instructions does:

FROM tells Docker that you want to base your image on the existing image called microsoft/dotnet:1.1.2-sdk. This image already contains all the dependencies for running .NET Core on Linux, so you don’t have to worry about setting those up.

COPY and WORKDIR copy the current directory’s contents into a new directory inside the container called /app, and set that to the the working directory for the subsequent instructions.

RUN executes dotnet restore and dotnet build, which restores the packages needed to run the ASP.NET Core application and compiles the project.

EXPOSE tells Docker to expose port 5000 (the default port for ASP.NET) on the container.

ENV sets the environment variable ASPNETCORE_URLS in the container. This will ensure that ASP.NET Core binds to the correct port and address.

ENTRYPOINT specifies the command to execute when the container starts up. In this case, it’s dotnet run.

Creating the Docker image

Once you’ve created the Dockerfile, you’re ready to build an image:

docker build -t genocs:aspnetcorehelloworld .

See that trailing period? That tells docker to look in the current directory for a Dockerfile. The -t flag tags the image with tag genocs:aspnetcorehelloworld.

When the image finishes building, you can spin it up:

docker run -d -p 8083:5000 -t genocs:aspnetcorehelloworld

docker run -it --rm the_image_you_built ls -l /usr/local/bin

The -d flag tells Docker to run the container in detached mode (in the background). The -p flag will map port 8080 on the host machine to port 5000 inside the container. Finally, the -t flag is used to specify which image to run.

That’s it!

You should see your container running when you check docker ps. Open up a web browser and navigate to http://localhost:8080. You should see the welcome page

How install RabbitMQ container

L'immagine ufficiale di RabbitMQ gira su una macchina linux. Quindi assicurarsi che il server giri in modalità Linux

RabbitMQ container RabbitMQ

docker build -t my_test .

docker run -d --hostname hpn-rabbit-host --name hpn-rabbit rabbitmq:3
docker run -d --hostname hpn-rabbit-host --name hpn-rabbit rabbitmq:3-management 
docker run -d --hostname hpn-rabbit-host --name hpn-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management
docker run -d --hostname hpn-rabbit-host --name hpn-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management
docker logs  hpn-rabbit
docker build -t genocs_rabbit .
docker run -d --hostname genocs_rabbit-host --name genocs-rabbit -p 15672:15672 -p 5672:5672 genocs_rabbit:latest
docker logs genocs-rabbit

I seguenti comandi eseguono il container utilizzando la rete locale e facendo il forward delle porte. Nota: La rete locale deve essere stata creata

docker run -d --hostname hpn_rabbit_host --name hpn_rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management --network utu_server_back
docker logs some-rabbit

Very useful example about RabbitMQ https://github.com/Gsantomaggio/rabbitmqexample

How to clear everything

#!/bin/bash
docker rm -f $(docker ps -a -q)
docker volume rm $(docker volume ls)
docker rmi -f $(docker images -q)
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
docker rmi $(docker images -q) -f

docker container prune
docker network prune
docker volume prune

docker container ls
docker network ls
docker volume ls

Build steps

The following commands:

  1. Build the image called genocs.my_image
  2. Create the tag
  3. Push the image to the Docker image repository (Docker Hub)
  4. Run the image using a network* (please check if the network exist otherwise create it)

** The network is optional so you can remove it if not requred

docker build -t genocs.my_image .
docker tag genocs.my_image genocs/my_image
docker push genocs/my_image
docker run -d --name my_image_instance_1 -p 5000:5001 genocs/my_imagee --network genocs-network