how to create directory in docker from terminal from zsh terminal? - macos

so i have downloaded Docker Desktop and until now i have tested out containers and stuff just executing regular commands (docker ps, docker images..., docker run...) inside my zsh terminal and it works fine but now i am in a position where i want to create a directory inside docker host so that i can put my dockerfile inside, but if i run mkdir directory-name it is going to create the directory inside my mac not docker! so what command can i use to indicate that i want the directory to be created on docker not on my own mac machine?

While your docker container is running, you can start a new shell session inside using docker exec.
docker exec -it mycontainer bash
-i means interactive - so you can type
-t allocates a pseudo-TTY - just know that you need the argument
Then inside this bash, you can create folders and files all you want and they will be placed inside your running container. Note that whenever you remove the container (e.g. to update its image), these changes will be entirely lost. For persistence, use docker volumes.

Say you have the following directory structure:
.
├── Dockerfile
└── simple-web-app
Your Dockerfile:
FROM scratch
ADD simple-web-app simple-web-app
Then you would run
docker build .

Related

Mont a volume in host directoy

I am running an application with a dockerfile that I made.
I run at first my image with this command:
docker run -it -p 8501:8501 99aa9d3b7cc1
Everything works fine, but I was expecting to see a file in a specific folder of my directory of the app, which is an expected behaviour. But running with docker, seems like the application cannot write in my host directory.
Then I tried to mount a volume with this command
docker 99aa9d3b7cc1:/output .
I got this error docker: invalid reference format.
Which is the right way to persist the data that the application generates?
Use docker bind mounts.
e.g.
-v "$(pwd)"/volume:/output
The files created in /output in the container will be accessible in the volume folder relative to where the docker command has been run.

How to tell docker run to run the latest image from docker hub?

Im using the following command to run an image off docker hub
docker run -it -e "SPRING_PROFILES_ACTIVE=localdocker" -p 8080:8080 name/repo:8 /bin/bash
Two questions:
First of all, is there a way to not have to keep increment the number next to the repo each time CI uploads a new image? Is there a way to say just run the latest?
What is the command to run if I want to stop the current instance and instead restart it with a newer image from the hub?
Yes, if you are using any CI tool then you can dynamically substitute the new created docker image tag to a variable.
Say - dockerimage:${buildID} , you can replace this buildID dynamically via your CI tool
While creating container, always give it a name, such that you can execute
docker stop
command on that container, to stop it and then clear the container permanently too, if you want to reuse that container name always, if you execute
docker ps -a | grep give_container_name_here
it will give you that container and then you can delete that via
docker rm $(docker ps -a | grep give_container_name_here)
Then you can restart docker container with new docker image that you have created via CI pipeline

How I can Dockerize my web api on windows

Docker is a full development platform for creating containerized apps, and Docker for Windows is the best way to get started with Docker on Windows systems.
Start your favorite shell (cmd.exe, PowerShell, or other) to check your versions of docker and docker-compose, and verify the installation.
PS C:\Users\Docker> docker --version
Docker version 17.03.0-ce, build 60ccb22
PS C:\Users\Docker> docker-compose --version
docker-compose version 1.11.2, build dfed245
Your questions is not very specific but it appears that you are trying to containerize an asp.net web app, Here is a basic clue to what you want to accomplish by using docker.
Docker is a linux containers system means it's based on linux kernel and by installing docker in windows you are installing a linux guest machine to built your containers in and you will customize your containers to forward ports that will serve your app development from inside the container to your host machine, So basically How this is going to happen? after installing docker first docker needs a base image(linux image) to run your containers from, so a great place to find docker images is docker hub, so also for a basic scenario you need:
1) Pull an image.
2) Run a container based on this image.
To accomplish number 1: we will use microsoft dotnet official docker hub as an example.
docker pull microsoft/aspnetcore
docker pull: will pull the dotnet:latest image from docker hub, :latest is a tag specify the latest stable release of dotnet means if you want another runtime version you will use docker pull dotnet:runtime from the above dotnet official docker hub link you will find tags under Supported tags
To accomplish number 2: we need to run a container by using this image.
docker run -d -p 8000:80 --name firstwebapptest microsoft/aspnetcore
docker run: will create a container name firstwebapptest based on microsoft/aspnetcore forwarding the container port 80to the host port 8000 and all of that will run as a detached mode -d
And now check your browser localhost:8000
This is a very basic scenario using the docker command line tools.
So another way to accomplish this scenario is by using a dockerfile you will find How to use this image in microsoft dotnet official docker hub link, It assumes that you already in your app directory that contain your compiled myapp.dll. What will you do is create a file called dockerfile in this directory and write this inside:
FROM microsoft/aspnetcore
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "myapp.dll"]
FROM: base image that we already pulled
WORKDIR: that will be the directory inside the linux container
COPY: . . the first . is copying your host directory content inside the container the second . is your guest directory in that case will be /app
ENTREYPOINT: is the linux command that will run once this container is up and running in that case dotnet myapp.dll means you are running the command dotnet from the linux container inside the WORKDIR /app with all your host directory app structure that contains your compiled myapp.dll. that we already copied it COPY . .
so now we have the dockerfile all what we need is to build and run it.
docker build -t secondwebapptest .
docker run -d -p 8001:80 secondwebapptest
docker build: will build a container named -t secondwebapptest from . the dot refer to the dockerfile that you just built and that you are already in the working directory otherwise you have to specify a path to the docker file by using -f but that is not our case.
docker run: will run the container that already been created that named secondwebapptest based on forwarding the container port 80to the host port 8001 and all of that will run as a detached mode -d.
And now check your browser localhost:8001

Saving Docker container state on Windows

I create a docker container using
docker-machine.exe create -d virtualbox --virtualbox-memory 2048 default
and I logged into the bash using
docker run -ti ubuntu /bin/bash
and I got something like root#ae78cd536ddf:/# where I did a couple of apt-get installs.
Then, I exited from the bash and when I again logged back in I could not find what I had installed. I wanted to do a docker commit, but I somehow can't figure where my installed stuff is?
UPDATE
Based on the answers I tried creating an image of the container. I have compiled all commands in a gist.
With docker run, you create a container from the ubuntu image. The container has the name ae78cd536ddf (in your case). You can inspect images and containers with docker ps -a and docker images respectively.
Each time you run docker run, a new container is created. When using docker run --name Somename, you force the container to be named Somename which prevents you from creating another container with the same name.
Images are immutable which means you can not change them. So when you modify something in the running container, the image stays the same and this you can create more containers from the same image.
So after you stopped a container (docker stop, exit the containerized bash or just reboot), you can run docker start ae78cd536ddf to restart it. But it will be running in background and you won't have a bash (check docker ps to see it's running). Now you just need a bash: docker exec -it ae78cd536ddf /bin/bash will execute a bash in the container you started before.
Just a note about creating images. You might want to install the software you always need (I personally love vim, htop, ...) and then docker commit the container. This will create a new image which you can see in docker images. Now you can run containers from this image by replacing ubuntu with your image name.
To get more reproducable builds (when using a CI for example), you can create a Dockerfile and run docker build.
Every docker run command creates a new container. The id in the hostname of the bash shell is the container ID. You can commit that.
To see all containers (including stopped containers), do docker ps -a.

What is a simple workflow to use docker in Windows with a basic file sharing possibility?

For the sake of simplicity, use ubuntu image as an example.
I often find it easier to use docker-compose, particularly if there's a high chance I'll want to both mount-volumes and link the container to another container at some point in the future.
Create a folder for working in, say "ubuntu".
In the "ubuntu" folder, create another folder called "files"
Create a file in that folder called "docker-compose.yml". In this file, enter:
ubuntucontainer:
image: "ubuntu:latest"
ports:
- "80:80"
volumes:
- ./files:/files
Whenever you need to start the box, navigate to "ubuntu" and type docker-compose up. To stop again, use docker-compose stop.
The advantage of using docker compose is that if you ever want to link-up a database container this can be done easily by adding another container to the yaml file, and then in the ubuntucontainer container adding a links section.
Not to mention, docker-compose up is quite minimal on the typing.
(Also, forwarding the ports with 80:80 may not be strictly necessary, it depends on what you want the box to do.)
TL;DR version:
Open Docker Quickstart Terminal. If it is already open, run $ cd ~
Run this once: $ docker run -it -v /$(pwd)/ubuntu:/windows --name ubu ubuntu
To start every time: $ docker start -i ubu
You will get an empty folder named ubuntu in your Windows user directory. You will see this folder with the name windows in your ubuntu container.
Explanation:
cd ~ is for making sure you are in Windows user directory.
-it stands for interactive, so you can interact with the container in the terminal environment. -v host_folder:container_folder enables sharing a folder between the host and the container. The host folder should be inside the Windows user folder. /$(pwd) translates to //c/Users/YOUR_USER_DIR in Windows 10. --name ubu assigns the name ubu to the container.
-i stands for interactive

Resources