can't list file in docker in docker (dind) - docker

I face this strange issue and can't explain why.
$ docker run -d --name dind --privileged --net=host -v `pwd`:/app -w /app docker:stable-dind
fe66d6e7e5effcf15e439a332a2368fddab810e9bc8ac3445392c8e56b0aa38a
$ docker exec dind ls
Dockerfile
$ docker exec dind docker run -v `pwd`:/app -w /app alpine ls
$ docker exec dind docker build -t demo .
Sending build context to Docker daemon 521.7kB
Step 1/24 : FROM alpine
So why I can't see my files in docker container which running in docker?
Why it can read the file Dockerfile with command docker build, but not docker run?

This is because pwd in your code will be parsed in your host machine, not in the container, so the container which in the container get the current directory of host machine, not current directory of container machine, you can prove it by change following command from:
$ docker exec dind docker run -v `pwd`:/app -w /app alpine ls
to
$ docker exec dind docker run -v /app:/app -w /app alpine ls
Then, you will see your Dockerfile output. FYI.

Related

Copy files from container to local using shell script

I m trying to write a shell script that builds/runs containers and then copies files from docker container to host.
docker build . -t container:latest
docker run -t -d container /bin/bash
docker cp container_id:/xyz/xyz.txt /tmp
How can I capture the container id from the build and then use it within the shell script? Thanks for your help.
The first option would be to simply store the container ID in a variable.
docker build . -t container:latest
container_id="$(docker run -t -d container /bin/bash)"
docker cp "$container_id":/xyz/xyz.txt /tmp
Docker also allows you to specify a container name.
docker build . -t container:latest
docker run -t --name NAME -d container /bin/bash
docker cp NAME:/xyz/xyz.txt /tmp

Why the customized docker container cannot connect to the Docker daemon at tcp://docker:2375?

For CI/CD purposes, I need docker in docker:
docker network create some-network
docker volume create some-docker-certs-ca
docker volume create some-docker-certs-client
docker run --privileged --name some-dind -d --network some-network --network-alias docker -e DOCKER_TLS_CERTDIR=/certs -v some-docker-certs-ca:/certs/ca -v some-docker-certs-client:/certs/client docker:dind
Now if I run the docker:latest image with -it option, I can use docker in the container as expected:
docker run --rm -it --network some-network -e DOCKER_TLS_CERTDIR=/certs -v some-docker-certs-client:/certs/client:ro docker:latest sh
/ # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
/ #
But I need to customize docker:latest image in a Dockerfile:
FROM docker:latest
# install package1
# install package2
# install package3
...
# install package4
And build the customized docker image:
docker build -t customized-docker .
But when I run the customized-docker image with -it options, It cannot connect to docker daemon:
docker run --rm -it --network some-network -e DOCKER_TLS_CERTDIR=/certs -v some-docker-certs-client:/certs/client:ro customized-docker:latest sh
/ # docker ps
Cannot connect to the Docker daemon at tcp://docker:2375. Is the docker daemon running?
/ #
What is the problem? Isn't there any way I can use customized docker image?
Edit:
I found a better way to do my CI/CD without "docker in docker". As David Maze mentioned, most of the times, there is no need to use "docker in docker". But my question still remains: Why can the docker:latest container connect to docker daemon but a customized docker container cannot.

How to pass in stdin input to a go app from dockerimage

I am still not well versed with docker so apologies in advance.
I need to run this package: https://github.com/sclevine/yj
What I have done so far is
pulled this image in my GOPATH (i don't think it was needed to be on GOPATH though).
Then i did docker build yj .
Now, I don't know how to execute the command to convert github yaml to hcl. I have tried following commands but all gave "help" message from the program
docker run -it <image_id> /bin/bash
docker run -it <image_id> /bin/yj
docker run -it <image_id>
docker run -it <image_id> -yc
docker run -it <image_id> /bin/yh -yc
docker run -it <image_id> /bin/yh
docker run -it --entrypoint /bin/bash ad5d67b05c22
docker run -it <image_id> -xyc
docker run -it <image_id> yj -yc
Few commands like docker run -it <image_id> yj -yc, didn't show any error. the cursoer stayed in console (without any prompt). I tried pasting my yaml file but nothing happened.
There are a few things to clarify here:
Your build command has syntax error and it should be something like docker build -t yj . which will build a new image with name yj and tag latest
Whenever you run docker run -it <image_id> /bin/bash it will create a new container and you will have to explicitly remove it. You can see all those containers using docker ps -a. For one-off usage, please add a flag --rm so that docker will remove the container whenever the container exits.
Once the image yj has been built, here are some of the commands that you can run to see how it works
docker run --rm -i yj <<EOF
key: value
EOF
or
echo key: value | docker run --rm -i yj

How can I install Docker inside an alpine container?

How can I install Docker inside an alpine container and run docker images?
I could install, but could not start docker and while running get "docker command not found error".
Dockerfile for running docker-cli inside alpine
FROM alpine:3.10
RUN apk add --update docker openrc
RUN rc-update add docker boot
Build docker image
docker build -t docker-alpine .
Run container (host and the alipne container will share the same docker engine)
docker run -it -v "/var/run/docker.sock:/var/run/docker.sock:rw" docker-alpine:latest /bin/sh
All you need is to install Docker CLI in an image based on Alpine and run the container mounting docker.sock. It allows running sibling Docker containers using host's Docker Engine. It is known as Docker-out-of-Docker and is considered a good alternative to running a separate Docker Engine inside a container (aka Docker-in-Docker).
Dockerfile
FROM alpine:3.11
RUN apk update && apk add --no-cache docker-cli
Build the image:
docker build -t alpine-docker .
Run the container mounting the docker.sock (-v /var/run/docker.sock:/var/run/docker.sock):
docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock alpine-docker docker ps
The command above should successfully run docker ps inside the Alpine-based container.

I cannot customize the dind (Docker IN Docker)

The dind (Docker IN Docker) pulled from DockerHub works fine, but I cannot build the dind from scratch.
I tried to build as follows.
My Docker version is 1.1.
$ git clone https://github.com/docker-library/docker
$ cd docker/
$ cd 1.1/
$ docker build -t docker:dind .
I could create a "dind" Docker image. After that I tried to run.
$ docker run -it --privileged --name test -d docker:dind
52e590b6636b3726bbe9774627f4424c2b9f8958a745d57c27d04cbec77a2d7b
$ docker run -it --rm --link test:docker docker run -it ubuntu bash
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
The dind did not work well.
So, I tried to customize from the dind pulled from Docker Hub.
$ docker pull docker:dind
$ docker run -it docker:dind ash
/ # exit
$ docker commit d508c2fd7131 docker:dind
sha256:f20e0314f996fe9f66806df47c1bdff956c84d11a6bfe2ff66279bee968323ec
$ docker run -it --privileged --name test -d docker:dind
d877c1993275fd4039b749f52d60a3095d40d52e13255c4fd88a319ca7ec306a
$ docker run -it --rm --link test:docker docker run -it ubuntu bash
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
It also had same problem. I just run the "dind" on Docker and exit immediately.
I cannot understand why I cannot customize the "dind" image.
Please tell me how to fix this problem.
Try this.
docker run --privileged -d --name test docker:dind
docker exec -it test docker version
docker commit test mydind
You can use mydind image

Resources