Here is how we containerize an application in this example I have chosen a dotnetcore application.
Add a Dockerfile which will be used to create an image for your application. Below is the sample.
FROM microsoft/dotnet:2.2-sdk AS build this basically says create our application based on this Microsoft image.
Navigate the shell to your application folder and then using docker run create an image for your application.
docker build -t sampleapp.
you can see your image using docker images. Finally that you have your image ready we can create as many container we need using this.
There are two options to create a container out of the image you just created either use this below command
docker run -d -p 8080:80 --name sampleapp appcontainername
or use the docker-compose.yml file from the app folder. Which consists the exact same information as yml file. Then use
docker-compose up
in the application folder from the shell.
now you must be able to see you web page on docker's ip and your port 8080.
Some useful docker commands
default is the docker machine's name in this example.
docker-machine regenerate-certs default
Every time our machine restarts DOCKER_MACHINE(environment variable) gets new ip and certs needs to be regenerated using this command.
docker-machine ls
will list the docker machines.
docker machine env default
will list the environment variables required for the machine.
docker-machine env default --shell bash
will get the command to run to set the environment variable using bash as shell in this sample.
eval $("C:\Users\bitsmonkey\bin\docker-machine.exe" env default --shell bash)
will set the environment variables required for docker to use this machine.
you just Containerized!!!
Photo by frank mckenna on Unsplash
Originally Posted on BitsMonkey
Top comments (2)
Good stuff!
As a next step you could take a look at creating a multi-stage build and running on one of the optimized images like
2.2-runtime
or2.2-runtime-deps
(hub.docker.com/r/microsoft/dotnet/).
Yeah sure thank you for that advice.