简体   繁体   中英

Customize shell startup in Linux Alpine docker container

My Dockerfile

FROM node:lts-alpine
RUN echo echo Hi Bob >> /etc/profile

Run

docker build -t node5 .
docker run -it --rm node5 sh

I'm expecting to see "Hi Bob" output in the terminal. It's not there.

How do on this distro do I run a command automatically when opening a terminal?

UPDATE 1

Tried this too. No joy.

RUN echo echo HI Bob >> /root/.profile

UPDATE 2

Following Joe Perri's link I find that this works using

docker run -it --rm node5 sh -l

So it just requires the terminal to be configured as a login console to work.

However opening the same container as a dev container in VsCode with the following.devcontainer.json config still fails.

{
   "build": {
      "dockerfile": "./Dockerfile"
   },
   "userEnvProbe": "loginShell"
}

My motivation is that I'm trying to construct a project such that I can spin up a stack of services in Docker containers using docker-compose up but then also connect to any of those containers using VsCode.

This mostly works. The only wrinkle is that many of these service containers will be running dev processes internally eg nodemon , ng serve etc, and while VsCode connects to the container ok it doesn't connect its terminal to the instance already running the dev process.

I'm trying to wire this up by launching the original dev process in a tmux session, and then automatically running a command to reconnect to that tmux session when VsCode connects to the container. This works great if I reconnect to the session manually, the only issue is that when VsCode connects to the container I can't get it to run the command to reconnect the session automatically (or any other command for that matter).

RUN runs when create docker image, which runs in container is CMD . So:

FROM node:lts-alpine
CMD ["echo", "Hi Bob"]

Hi Bob will print in terminal. Your Dockerfile just makes text file in docke image.

what about using bash?

FROM node:lts-alpine
RUN apk add bash
RUN echo echo Hi Bob >> /etc/profile

and then

docker build -t node5 .
docker run -it --rm node5 bash

Hi Bob
bash-5.0#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM