Add Environment variable to Docker file that Contains : - docker

I want to add some environment variables to dockerfile which contains :
SO I need to add something like
environment:
-OAuth2Configuration:CacheProvider=true
any idea how to do that , I even tried to surround the key with "" but it fails to so if any idea , and docker compose file it gives error on :

Use env_file option of docker-compose.
Check this out.
Here is what I tried and it worked:
Created docker-compose.yaml file.
version: '3'
services:
distro:
env_file: test.env
image: alpine
restart: always
container_name: Alpine_Distro
entrypoint: tail -f /dev/null
Created test.env file.
OAuth2Configuration:CacheProvider=true
Ran docker-compose up -d
$ docker-compose up -d
Creating network "ttt_default" with the default driver
Pulling distro (alpine:)...
latest: Pulling from library/alpine
921b31ab772b: Pull complete
Creating Alpine_Distro ... done
[node1] (local) root#192.168.0.33 ~/ttt
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74ee753a27b6 alpine "tail -f /dev/null" 4 seconds ago Up 2 seconds Alpine_Distro
[node1] (local) root#192.168.0.33 ~/ttt
$ docker exec -it 74ee753a27b6 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=74ee753a27b6
TERM=xterm
OAuth2Configuration:CacheProvider=true
HOME=/root
[node1] (local) root#192.168.0.33 ~/ttt
NOTE: As you can see OAuth2Configuration:CacheProvider=true env variable is properly set.
Here your environment variable contains : in it, so I guess that's why environment field was not working for you. In env_file option whatever is on left hand side of = is considered as environment name and right hand side -f = as the value. So its key=value syntax in env_file that's why it will work.
Hope this helps.
Update:
In case you are using only plain docker use --env-file option of docker run
$ docker run -itd --env-file test.env alpine
74f60cb6f513519c2dd7a093622537215937db1682b79a838c95e944a649f451
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74f60cb6f513 alpine "/bin/sh" 12 seconds ago Up 10 seconds infallible_nobel
$ docker exec -it 74f60cb6f513 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=74f60cb6f513
TERM=xterm
OAuth2Configuration:CacheProvider=true
HOME=/root

Try to put quotes, I checked it with:
FROM alpine:latest
ENV "OAuth2Configuration:CacheProvider"=true
CMD ["env"]

Related

DOCKER: Mysql dockerfile remains in exited state after "docker run"

I have to start a mysql container through a dockerfile, in which I simply have to set the environment variables, I wrote the dockerfile like this, but when I do the "docker run" it remains in exited state.
FROM mysql
ENV DB_HOST=localhost
ENV DB_NAME=productsdb
ENV DB_USER=root
ENV DB_PWD=mm22
ENV DB_DIALECT=mysql
ENV SERVER_PORT=5000
ENV DB_PORT=3306
When you run the container, it outputs - in the log - the following message
You need to specify one of the following as an environment variable:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
Basically, MySQL won't start without a root password or being told that no password is OK.
Add
ENV MYSQL_ROOT_PASSWORD=myrootpassword
to your Dockerfile and it'll run.
If there is no error in your container add option -d to run container background.
docker run -d yourMysqlImage:yourTag
If you didn't build image yet.
docker build -f yourDockerfile
And you should check container logs to see what happend.
List all container:
docker ps -a
Check logs:
docker logs yourContainerId
Here is my quick start command:
docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root mysql:latest

Start a container in interactive shell in docker compose [duplicate]

Is there any way to start an interactive shell in a container using Docker Compose only? I've tried something like this, in my docker-compose.yml:
myapp:
image: alpine:latest
entrypoint: /bin/sh
When I start this container using docker-compose up it's exited immediately. Are there any flags I can add to the entrypoint command, or as an additional option to myapp, to start an interactive shell?
I know there are native docker command options to achieve this, just curious if it's possible using only Docker Compose, too.
You need to include the following lines in your docker-compose.yml:
version: "3"
services:
app:
image: app:1.2.3
stdin_open: true # docker run -i
tty: true # docker run -t
The first corresponds to -i in docker run and the second to -t.
The canonical way to get an interactive shell with docker-compose is to use:
docker-compose run --rm myapp
(With the service name myapp taken from your example. More general: it must be an existing service name in your docker-compose file, myapp is not just a command of your choice. Example: bash instead of myapp would not work here.)
You can set stdin_open: true, tty: true, however that won't actually give you a proper shell with up, because logs are being streamed from all the containers.
You can also use
docker exec -ti <container name> /bin/bash
to get a shell on a running container.
The official getting started example (https://docs.docker.com/compose/gettingstarted/) uses the following docker-compose.yml:
version: "3.9"
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
After you start this with docker-compose up, you can shell into either your redis container or your web container with:
docker-compose exec redis sh
docker-compose exec web sh
docker-compose run myapp sh should do the deal.
There is some confusion with up/run, but docker-compose run docs have great explanation: https://docs.docker.com/compose/reference/run
If anyone from the future also wanders up here:
docker-compose exec service_name sh
or
docker-compose exec service_name bash
or you can run single lines like
docker-compose exec service_name php -v
That is after you already have your containers up and running.
The service_name is defined in your docker-compose.yml file
Using docker-compose, I found the easiest way to do this is to do a docker ps -a (after starting my containers with docker-compose up) and get the ID of the container I want to have an interactive shell in (let's call it xyz123).
Then it's a simple matter to execute
docker exec -ti xyz123 /bin/bash
and voila, an interactive shell.
This question is very interesting for me because I have problems, when I run container after execution finishes immediately exit and I fixed with -it:
docker run -it -p 3000:3000 -v /app/node_modules -v $(pwd):/app <your_container_id>
And when I must automate it with docker compose:
version: '3'
services:
frontend:
stdin_open: true
tty: true
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "3000:3000"
volumes:
- /app/node_modules
- .:/app
This makes the trick: stdin_open: true, tty: true
This is a project generated with create-react-app
Dockerfile.dev it looks this that:
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
Hope this example will help other to run a frontend(react in example) into docker container.
I prefer
docker-compose exec my_container_name bash
If the yml is called docker-compose.yml it can be launched with a simple $ docker-compose up. The corresponding attachment of a terminal can be simply (consider that the yml has specified a service called myservice):
$ docker-compose exec myservice sh
However, if you are using a different yml file name, such as docker-compose-mycompose.yml, it should be launched using $ docker-compose -f docker-compose-mycompose.yml up. To attach an interactive terminal you have to specify the yml file too, just like:
$ docker-compose -f docker-compose-mycompose.yml exec myservice sh
A addition to this old question, as I only had the case last time. The difference between sh and bash. So it can happen that for some bash doesn't work and only sh does.
So you can:
docker-compose exec CONTAINER_NAME sh
and in most cases: docker-compose exec CONTAINER_NAME bash
use.
If you have time. The difference between sh and bash is well explained here:
https://www.baeldung.com/linux/sh-vs-bash
You can do docker-compose exec SERVICE_NAME sh on the command line. The SERVICE_NAME is defined in your docker-compose.yml. For example,
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
The SERVICE_NAME would be "zookeeper".
According to documentation -> https://docs.docker.com/compose/reference/run/
You can use this docker-compose run --rm app bash
[app] is the name of your service in docker-compose.yml

Run docker-compose without installation

I try to run docker-compose without installation, so using docker:compose repository (with docker run).
So I tried this way :
docker run -ti --rm -v $PWD:/XXX docker/compose:1.24.1 up -d
The problem is that I don't know the container dir name of docker/compose (here XXX) to mount my current folder as volume.
Any ideas...?
Thanks !
You can bind mount your local docker-compose.yaml to any place just remember to tell docker-compose use -f, like next:
docker run -ti --rm -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}:/code docker/compose:1.24.1 -f /code/docker-compose.yaml up -d
Meanwhile, don't forget to add docker.sock of your host machine bind mount to the container.
That XXX folder can be anything inside the container. Basically in -v option of docker run. Its -v [host-directory]:[container-directory].
If you are trying to run a docker-compose up inside the container, then follow these steps:
Create a directory on host mkdir /root/test
Create docker-compose.yaml file with following contents:
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: redis
Run docker run command to run docker-compose inside the container.
docker run -itd -v /var/run/docker.sock:/var/run/docker.sock -v /root/test/:/var/tmp/ docker/compose:1.24.1 -f /var/tmp/docker-compose.yaml up -d
NOTE: Here /var/tmp directory inside the container will contain docker-compose.yaml file so I have used -f option to specify complete path of the yaml file.
Hope this helps.

How to ssh into the services create using docker-compose

This is my docker compose file
version: '2'
# based off compose-sample-2, only we build nginx.conf into image
# uses sample site from https://startbootstrap.com/template-overviews/agency/
services:
proxy:
build:
context: .
dockerfile: nginx.Dockerfile
ports:
- '80:80'
web:
image: httpd
volumes:
- ./html:/usr/local/apache2/htdocs/
Now can I ssh into any of the services which gets creats when I run docker-compose up
The standard mechanism is not to ssh into containers, but to connect to a container using docker exec. Given a container Id like 3cdb7385c127 you can connect (aka ssh into it) with
docker exec -it 3cdb7385c127 sh
or for a full login shell if you have bash available in container
docker exec -it 3cdb7385c127 bash -l
You can still ssh into a container if you want to, but you would need to have a ssh server installed, configured and running and you would need to have access to the container IP from outside or redirect container's :22 onto some port on the host.
The easiest way is to run the docker-compose exec command:
docker-compose exec web /bin/bash
with the latest version of docker, since "Docker Compose is now in the Docker CLI", you can do:
docker compose exec web /bin/bash
If you do want to use the pure docker command, you can do:
web=`docker container ls |grep web |awk '{print \$1}'`
docker container exec -it $web /bin/bash
or in a single line:
docker container exec -it `docker container ls |grep web |awk '{print \$1}'` /bin/bash
in which we find the container id first, then run the familiar docker container exec -it command.
As it is mentioned in the question, the service containers have been created by running docker-compose up. Therefore I don't think docker-compose run is appropriate answer, since it will start a container in my test.
do 'docker ps' to get the names and docker id for your container.
do 'docker exec -it <docker_id> /bin/bash'
this will give you bash prompt inside container.
If you specify container_name in your docker-compose.yaml file, you can use that to log in with the docker exec command.
Example:
django:
container_name: django
more_stuff: ...
Then, to log in to a running docker-compose:
docker exec -it django /bin/bash
This works better for me as I don't need to check the current running ID and it's easy for me to remember.
While using the docker command also works, the easiest way to do this which does not require finding out the ID of the container is using the docker-compose run subcommand, for example:
service="web"
docker-compose run $service /bin/bash

Execute command in linked docker container

Is there any way posible to exec command from inside one docker container in the linked docker container?
I don't want to exec command from the host.
As long as you have access to something like the docker socket within your container, you can run any command inside any docker container, doesn't matter whether or not it is linked. For example:
# run a container and link it to `other`
docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock \
--link other:other myimage bash -l
bash$ docker exec --it other echo hello
This works even if the link was not specified.
With docker-compose:
version: '2.1'
services:
site:
image: ubuntu
container_name: test-site
command: sleep 999999
dkr:
image: docker
privileged: true
working_dir: "/dkr"
volumes:
- ".:/dkr"
- "/var/run/docker.sock:/var/run/docker.sock"
command: docker ps -a
Then try:
docker-compose up -d site
docker-compose up dkr
result:
Attaching to tmp_dkr_1
dkr_1 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dkr_1 | 25e382142b2e docker "docker-entrypoint..." Less than a second ago Up Less than a second tmp_dkr_1
Example Project
https://github.com/reduardo7/docker-container-access
As "Abdullah Jibaly" said you can do that but there is some security issues you have to consider, also there is sdk docker to use, and for python applications can use Docker SDK for Python

Resources