I am trying to start up a Padrino console (similar to Rails console).
Here's my ENTRYPOINT command in Dockerfile
ENTRYPOINT /usr/src/app/docker-entrypoint.sh
I want to start the console when the env variable value is $CONSOLE = Y
Here's the docker-entrypoint.sh script
if [ "$CONSOLE" = "Y" ];
then
echo "Starting Padrino console"
bundle exec padrino console
else
#something else
fi
However, when I run docker-compose up with environment variable CONSOLE = Y, the console begins to start but ends with Switch to Inspect mode. I tried a lot of solutions online, and while docker-compose exec <container-name> bundle exec padrino console might work, it exceeds the memory and kills both the console process and the server running on the container.
How can I just start the console in the docker-compose up command?
EDIT
Here's my docker-compose.yml file
version: '3'
services:
app:
build: .
volumes:
- .:/usr/src/app
ports:
- "36081:36081"
restart: always
depends_on:
<other-services>
container_name: application
environment:
- MIGRATE=$MIGRATE
- CONSOLE=$CONSOLE
<other-services>
So according to this issue, you can not allocate tty in docker-compose because it starts multiple containers.
That is expected behaviour. up is not interactive. It can start
multiple containers, so you can't have a single terminal that has
stdin open for multiple containers.
as there is single service in your docker-compose so you can try with run command instead of up
docker-compose run app
or the other option is to start all the container then run with docker attach
but first, you need
docker-compose -d
then
docker attach container_name
you might need tty flag to true as well
version: '3'
services:
app:
build: .
tty: true
Related
The docker compose yml file below keeps the container open after I run docker compose up -d but command: bash does not get executed:
version: "3.8"
services:
service:
container_name: pw-cont
image: mcr.microsoft.com/playwright:v1.30.0-focal
stdin_open: true # -i
tty: true # -t
network_mode: host # --network host
volumes: # Ensures that updates in local/container are in sync
- $PWD:/playwright/
working_dir: /playwright
command: bash
After I spin the container up, I wanted to visit Docker Desktop > Running container's terminal.
Expectation: Since the file has command: bash, I expect that in docker desktop, when I go to the running container's terminal, it will show root#docker-desktop:/playwright#.
Actual: Container's terminal in docker desktop is showing #, still need to type bash to see root#docker-desktop:/playwright#.
Can the yml file be updated so that bash gets auto executed when spinning up the container?
docker compose doesn't provide that sort of interactive connection. Your docker-compose.yaml file is fine; once your container is running you can attach to it using docker attach pw-cont to access stdin/stdout for the container.
$ docker compose up -d
[+] Running 1/1
⠿ Container pw-cont Started 0.1s
$ docker attach pw-cont
root#rocket:/playwright#
root#rocket:/playwright#
I'm not sure what you are trying to achieve, but using the run command
docker-compose run service
gives me the prompt you expect.
I have an up and running containers and I wish to execute a database backup. Apparently, a simple command from the docker such as: sudo mkdir new_folder result in: bash: sudo: command not found
What have I tried (on an intuitive level) I accessed one of the running container with docker exec -i -t 434a38fedd69/bin/bash and RUN
apt-get update
apt-get install sudo
when exit back to docker and tried to perform sudo mkdir new_folder but I got the same message bash: sudo: command not found
Baresp#adhg MINGW64 /c/Program Files/Docker Toolbox/postgre
$ mkdir new_folder
mkdir: cannot create directory ‘new_folder’: Permission denied
Baresp#adhg MINGW64 /c/Program Files/Docker Toolbox/postgre
$ sudo mkdir new_folder
bash: sudo: command not found
BTW, I'm not sure if this is relevant but the docker-compose file I was using is:
version: '2'
services:
postgres:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: changeme
PGDATA: /data/postgres
volumes:
- /data/postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
container_name: xx_postgres
pgadmin:
links:
- postgres:postgres
image: fenglc/pgadmin4
volumes:
- /data/pgadmin:/root/.pgadmin
ports:
- "5050:5050"
networks:
- postgres
restart: unless-stopped
container_name: xx_pgadmin
networks:
postgres:
driver: bridge
First, nothing you do in a docker exec is persistent outside of that particular running container (copy of the image), so if you want future containers run from that image to include sudo, those apt-get commands need to go into the Dockerfile that builds the image. Which, since you're using docker-compose, would require you to first make a Dockerfile and specify its location in the YAML.
Second, what do you mean "exit back to docker"? Nothing you do inside a container is going to have any effect on the system that Docker itself is running on, but it looks like you're running software install commands inside a Docker container and then expecting that to result in the newly-installed software being available outside the container on the Windows system that is running Docker.
To do a backup of the postgres database in the container, you first have to enter the container (similar to how you do it):
docker exec -it postgres bash
(substitude postgres with the real container name you get from docker-compose ps)
Now you are in the container as root. That means, you don't need sudo for anything. Next create your backup folder:
mkdir /tmp/backup
Now run the backup command, from a quick Google I found the following (you might know better):
pg_dumpall > /tmp/backup/filename
Then exit the shell within the container by typing exit. From your host system run the following to copy the backup file out of the container:
docker cp postgres:/tmp/backup/filename .
(postgres is your container name again)
I am running a simple Hello World world container with docker-compose.
There should be a mounted folder (with my files) at /root/sharedFolder but this folder is empty.
I am running Docker on Ubuntu OS (on top of my Windows server). And this works on a normal Ubuntu machine.
Any ideas?
docker-compose.yaml
version: '3'
services:
web:
build: .
volumes:
- ".:/root/sharedFolder"
Dockerfile:
#FROM - Image to start building on.
FROM ubuntu:14.04
#MAINTAINER - Identifies the maintainer of the dockerfile.
MAINTAINER ian.miell#gmail.com
#RUN - Runs a command in the container
RUN echo "Hello world" > /root/hello_world.txt
#CMD - Identifies the command that should be used by default when running the image as a container.
CMD ["sleep", "400"]
Instead of doing echo in RUN, do it in CMD or ENRTYPOINT. RUN happens during the image build phase where as CMD happens when your container is up and running with the volumes.
RUN is done during image build step
CMD specifies the command executed by default when you run a built
image.
You can also achieve the same with ENTRYPOINT
Google docker RUN vs CMD for more details.
I have 2 containers mgmt-app who is a Meteor container and mgmt-mongo who is the MongoDB.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b65be4ac454 gitlab-lab:5005/dfc/mongo:latest "/entrypoint.sh mongo" About an hour ago Up About an hour 27017/tcp mgmt-mongo
dff0b3c69c5f gitlab-lab:5005/dfc/mgmt-docker-gui:lab "/bin/sh -c 'sh $METE" About an hour ago Up 42 minutes 0.0.0.0:80->80/tcp mgmt-app
From my Docker host I want to run docker run gitlab-lab:5005/dfc/mgmt-docker-gui:lab ls -al
but I have this error:
=> Starting meteor app on port:80
/app/programs/server/node_modules/fibers/future.js:280
throw(ex);
^
Error: MONGO_URL must be set in environment
So I tried:
docker run -e "MONGO_URL=mongodb://mgmt-mongo:27017/meteor" gitlab-lab:5005/dfc/mgmt-docker-gui:lab ls -al
and then the error was:
/app/programs/server/node_modules/fibers/future.js:313
throw(ex);
^
MongoError: failed to connect to server [mgmt-mongo:27017] on first connect
I really don't understand because when I do a docker-compose up -d with this file:
mgmt-app:
image: gitlab-lab:5005/dfc/mgmt-docker-gui:latest
container_name: mgmt-app
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- $HOME/.docker:/root/.docker
- /home/dockeradm/compose/area:/home/dockeradm/compose/area
environment:
- ROOT_URL=http://localhost:80
- MONGO_URL=mongodb://mgmt-mongo:27017/meteor
ports:
- 80:80
restart: always
mgmt-mongo:
image: gitlab-lab:5005/dfc/mongo:latest
container_name: mgmt-mongo
volumes:
- mgmt_mongo_data_config:/data/configdb
- mgmt_mongo_data_db:/data/db
restart: always
everything go well.
So my request is, how should I do my docker run to execute my command ? (the command is not a simple ls -al but it's ok for the demo)
When you run the containers separately with docker run, they are not linked on the same docker network so the mongo container is not accessible from the app container. To remedy this, you should use either:
--link to mark the app container as linked to the mongo container. This works, but is deprecated.
a defined docker network for both containers to be linked by; this is more complex, but is the recommended architecture
By contrast, docker-compose automatically adds both containers to the same docker network, so they are immediately connectable without any extra configuration required:
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
Running docker in docker image (dind), on windows 10. Powershell is run as admin. I have a manager and a worker up container up through docker-compose.yml file.
The compose yaml file is as such:
version: '2'
services:
manager:
image: docker:latest
ports:
- "2375"
- "8080"
privileged: true
tty: true
worker:
image: docker:latest
ports:
- "8080"
privileged: true
tty: true
I don't know why or what tty: true even does but it's the only way to get it to stay up for some reason.
I try to init the manager with:
docker-compose exec manager docker swarm init --listen-addr 0.0.0.0:2377
I also tried with the port being 0.0.0.0:2375 as what is open in the compose yaml.
When I run the command and get this:
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
Here is the result of a docker-compose ps
Name Command State Ports
swarmtest_manager_1 docker-entrypoint.sh sh Up 0.0.0.0:32782->2375/tcp, 0.0.0.0:32781->8080/tcp
swarmtest_worker_1 docker-entrypoint.sh sh Up 0.0.0.0:32780->8080/tcp
Running and testing services in the dind environment would be ideal, although I still don't like compose as I am trying to learn how to use it better, creating a docker-machine and using the docker swarm mode seems much easier, although I'm truthfully not sure of the limitations with compose in this manner.